package com.guigu.service.impl;

import com.guigu.dto.loginDTO;
import com.guigu.entity.Login;
import com.guigu.entity.Menus;
import com.guigu.entity.Roles;
import com.guigu.entity.t_permission;
import com.guigu.mapper.LoginMapper;
import com.guigu.service.LoginService;
import com.guigu.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.relation.Role;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class LoginServiceImpl implements LoginService {
    private static final String USER = "qianqiu_ya@163.com"; // 发件人称号，同邮箱地址※
    private static final String PASSWORD = "QOBTHWZGLGBADVYL"; // 授权码，开启SMTP时显示※

    @Autowired
    private LoginMapper loginMapper;

    @Override
    public String queryByLogin(Login login) {
        return this.loginMapper.queryByLogin(login);
    }

    @Override
    public UserVO findUserByName(String username) {
        UserVO user = this.loginMapper.findUserByName(username);
        System.err.println(user);
        List<Menus> menus = this.loginMapper.queryUserMenus(username);
        if(menus != null){
            user.setMenus(menus);
        }
        List<Roles> roles = this.loginMapper.queryUserRoles(username);
        if(roles != null){
            user.setRoles(roles);
        }
        List<Integer> permission = new ArrayList<>();
            for (Roles role : roles) {
                permission.add(role.getId());
            }
        List<t_permission> permissions = this.loginMapper.permission(permission);
            if(permissions != null){
                user.setPermissions(permissions);
            }
        return this.loginMapper.findUserByName(username);
    }

    public boolean sendMail(String to, String text, String title) {
        try {
            final Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
//            注意发送邮件的方法中，发送给谁的，发送给对应的app，※
//            要改成对应的app。扣扣的改成qq的，网易的要改成网易的。※
//            props.put("mail.smtp.host", "smtp.qq.com");
            props.put("mail.smtp.host", "smtp.163.com");

            // 发件人的账号
            props.put("mail.user", USER);
            //发件人的密码
            props.put("mail.password", PASSWORD);

            // 构建授权信息，用于进行SMTP进行身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用户名、密码
                    String userName = props.getProperty("mail.user");
                    String password = props.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };
            // 使用环境属性和授权信息，创建邮件会话
            Session mailSession = Session.getInstance(props, authenticator);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(mailSession);
            // 设置发件人
            String username = props.getProperty("mail.user");
            InternetAddress form = new InternetAddress(username);
            message.setFrom(form);

            // 设置收件人
            InternetAddress toAddress = new InternetAddress(to);
            message.setRecipient(Message.RecipientType.TO, toAddress);

            // 设置邮件标题
            message.setSubject(title);

            // 设置邮件的内容体
            message.setContent(text, "text/html;charset=UTF-8");
            // 发送邮件
            Transport.send(message);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }


    @Override
    public Integer loginqd(loginDTO dto) {
        return this.loginMapper.loginqd(dto);
    }
}
